package com.petecat.interchan.system.company.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mhuang.common.util.DataUtils;
import com.petecat.interchan.core.exception.BusinessException;
import com.petecat.interchan.core.service.impl.BaseServiceImpl;
import com.petecat.interchan.protocol.Result;
import com.petecat.interchan.protocol.base.project.vo.CompanySelectVO;
import com.petecat.interchan.protocol.data.Page;
import com.petecat.interchan.protocol.data.PageVO;
import com.petecat.interchan.protocol.sso.company.dto.SysCompanyAddDTO;
import com.petecat.interchan.protocol.sso.company.dto.SysCompanyQryDTO;
import com.petecat.interchan.protocol.sso.company.dto.SysCompanyUpdDTO;
import com.petecat.interchan.protocol.sso.company.vo.SysCompanyQryVO;
import com.petecat.interchan.protocol.sso.sysuser.SysUserAddDTO;
import com.petecat.interchan.redis.IdWorker;
import com.petecat.interchan.redis.commands.IRedisExtCommands;
import com.petecat.interchan.sso.sysuser.service.ISysUserService;
import com.petecat.interchan.system.company.entity.SysCompany;
import com.petecat.interchan.system.company.mapper.ICompanyMapper;
import com.petecat.interchan.system.company.service.ICompanyService;

@Service
public class CompanyServiceImpl extends BaseServiceImpl<SysCompany, String> implements ICompanyService {
	
	@Autowired
	private ICompanyMapper companyMapper;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
	private IRedisExtCommands redisExtCommands;
	
	@Autowired
	private Environment environment;
	
	private final static String SYUSER_REDIS_USERID_TO_USER_PREKEY="syur_user_uid_";
	/**
	 * 分页查询公司信息
	 */
	@Override
	public PageVO<SysCompanyQryVO> queryCompanyByPage(SysCompanyQryDTO companyQryDTO) {
		SysUserAddDTO sysUserAddDTO = redisExtCommands.hget(SYUSER_REDIS_USERID_TO_USER_PREKEY, companyQryDTO.getUserId(), SysUserAddDTO.class);
		if(null == sysUserAddDTO) {
			throw new BusinessException(Result.SYS_RESULT_FAILD,
					environment.getProperty("user_not_exites"));
		}
		String user = "0".equals(sysUserAddDTO.getIsMaster()) ? sysUserAddDTO.getUserid():null;
		companyQryDTO.setUserId(user);
		SysCompany sysCompany = DataUtils.copyTo(companyQryDTO, SysCompany.class);
		int count = this.companyMapper.count(sysCompany);
		PageVO<SysCompanyQryVO> pageVo = new PageVO<>();
		pageVo.setTotalSize(count);
		if(count > 0) {
			Page<SysCompany> page = new Page<>();
			page.setRows(companyQryDTO.getRows());
			page.setStart(companyQryDTO.getStart());
			page.setRecord(sysCompany);
			List<SysCompany> companyList = this.companyMapper.page(page);
			pageVo.setResult(DataUtils.copyTo(companyList, SysCompanyQryVO.class));
		}
		return pageVo;
	}
	
	/**
	 * 新增公司基本信息
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveCompany(SysCompanyAddDTO sysCompanyAddDTO, String userId) {
		SysCompany sysCompany = DataUtils.copyTo(sysCompanyAddDTO, SysCompany.class);
		sysCompany.setCreateTime(new Date());
		sysCompany.setOperatorTime(new Date());
		sysCompany.setId(String.valueOf(new IdWorker().nextId()));
		sysCompany.setOperaorUserId(userId);
		sysCompany.setUserId(userId);
		sysCompany.setStatus("1");
		this.companyMapper.save(sysCompany);
	}

	/**
	 * 修改公司状态
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteCompanyById(String companyId, String state) {
		this.companyMapper.updateCompanyStateById(companyId, state);
	}

	/**
	 * 修改公司基本信息
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void modifyCompanyById(SysCompanyUpdDTO sysCompanyUpdDTO, String userId) {
		SysCompany sysCompany = DataUtils.copyTo(sysCompanyUpdDTO, SysCompany.class);
		sysCompany.setOperaorUserId(userId);
		sysCompany.setOperatorTime(new Date());
		this.companyMapper.update(sysCompany);
	}
	
	/**
	 * 根据登录用户下拉查询树形公司
	 */
	@Override
	public List<CompanySelectVO> queryCompanySelectTreeByUserId(String userId) {
		String sysUserById = sysUserService.getSysUserById(userId);
		//如果为0则不是管理员
		String user = "0".equals(sysUserById)? userId:null;
		List<CompanySelectVO> companyVO = this.companyMapper.queryCompanySelectTreeByUserId(user);
		if(!companyVO.isEmpty()) {
			return  companyVO;
		}
		return null;
	}
	
	/**
	 * 查询所有公司
	 */
	@Override
	public List<SysCompany> queryAllCompany() {
		List<SysCompany> list = this.companyMapper.queryAllCompany();
		if(!list.isEmpty()) {
			return list;
		}
		return null;
	}
	
}
